package cn.edu.wit.dao;

import cn.edu.wit.beans.Clazz;
import cn.edu.wit.utils.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ClazzDAO {
	
	/**
	 * ��Ӱ༶��Ϣ
	 * @param c װ�а༶��Ϣ��Clazz����
	 * @return	�༶��Ϣ��ӳɹ�����true,���򷵻�false
	 */
	public boolean insertClass(Clazz c){
		boolean flag = false;
		try {
			Connection conn = DBManager.getConn();
			String sql = "insert into tb_classes(cname,count,leader,remark) values(?,0,?,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, c.getCname());
			ps.setString(2, c.getLeader());
			ps.setString(3, c.getRemark());
			int i = ps.executeUpdate();
			flag = i>0?true:false;
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean deleteClass(int cid){
		boolean flag = false;
		try {
			Connection conn = DBManager.getConn();
			String sql = "delete from tb_classes where cid=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			int i = ps.executeUpdate();
			flag = i>0?true:false;
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean updateClass(Clazz c){
		boolean flag = false;
		try {
			Connection conn = DBManager.getConn();
			String sql = "update tb_classes set cname=?,leader=?,remark=? where cid=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, c.getCname());
			ps.setString(2, c.getLeader());
			ps.setString(3, c.getRemark());
			ps.setInt(4, c.getCid());
			int i = ps.executeUpdate();
			flag = i>0?true:false;
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public Clazz queryClass(int cid){
		Clazz c = null;
		try {
			Connection conn = DBManager.getConn();
			String sql = "select * from tb_classes where cid=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				String cname = rs.getString("cname");
				int count = rs.getInt("count");
				String leader = rs.getString("leader");
				String remark = rs.getString("remark");
				c = new Clazz(cid, cname, count, leader, remark);
			}
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c;
	}
	
	public List<Clazz> listClasses(){
		List<Clazz> clas = new ArrayList<Clazz>();
		try {
			Connection conn = DBManager.getConn();
			String sql = "select * from tb_classes";
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				int cid = rs.getInt("cid");
				String cname = rs.getString("cname");
				int count = rs.getInt("count");
				String leader = rs.getString("leader");
				String remark = rs.getString("remark");
				Clazz c = new Clazz(cid, cname, count, leader, remark);
				
				clas.add(c);
			}
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return clas;
	}
	
	/**
	 * 
	 * @param cid
	 * @param type type=1��ʾ�༶����+1��type=-1���ʾ�༶����-1
	 */
	public boolean changeCount(int cid,int type){
		boolean flag = false;
		try {
			Connection conn = DBManager.getConn();
			String sql = null;
			if(type==1){
				sql = "update tb_classes set count=count+1 where cid=?";
			}
			if(type==-1){
				sql = "update tb_classes set count=count-1 where cid=?";
			}
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			int i = ps.executeUpdate();
			flag = i>0?true:false;
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	
}
